// import util from "../../../utils/YCSDKJS/util";
const setAPI = requirePlugin("hello-plugin")
const HealthContentAPI = requirePlugin("hello-plugin")
const util = require("../../../utils/util");
const {
	handleToast,
	closeToast
} = require("../../../utils/alertUtil")
const constant = require("../../../utils/constant")
const date = new Date();
const years = [];
const months = [];
const days = [];
const hours = [];
const minutes = [];
const second = [];
//获取年
for (let i = 2018; i <= date.getFullYear() + 5; i++) {
	years.push("" + i);
}
//获取月份
for (let i = 1; i <= 12; i++) {
	if (i < 10) {
		i = "0" + i;
	}
	months.push("" + i);
}
//获取日期
for (let i = 1; i <= 31; i++) {
	if (i < 10) {
		i = "0" + i;
	}
	days.push("" + i);
}
//获取小时
for (let i = 0; i < 24; i++) {
	if (i < 10) {
		i = "0" + i;
	}
	hours.push("" + i);
}
//获取分钟
for (let i = 0; i < 60; i++) {
	if (i < 10) {
		i = "0" + i;
	}
	minutes.push("" + i);
}
//获取秒
for (let i = 0; i < 60; i++) {
	if (i < 10) {
		i = "0" + i;
	}
	second.push("" + i);
}
Page({
	data: {
		time: '',
		multiArray: [years, months, days, hours, minutes, second],
		multiIndex: [0, 9, 16, 10, 17, 1],
		choose_year: '',
		timeFlag: false,
		nowData: [],
		canvasText: '接收到蓝牙设备信息：',
		locationFlag: false,
		antiLostFlag: false,
		heartLineFlag: false,
		heartLineMonitoringFlag: false,
		languageFlag: false,
		brightScreenFlag: false,
		brightnessFlag: false,
		notDisturbFlag: false,
		temperatureFlag: false,
		temperatureModeFlag: false,
		tmperatureModeFlag: false,
		stepGoalFlag: false,
		sleepGoalFlag: false,

		isEnableAntiLost: true,
		watchLocation: true,
		isEnableheartLineAlert: false,
		isEnableHeartLineMonitoringAlert: true,
		isEnableTemperatureAlert: true,
		currentLanguage: "中文",
		maxHeartLine: 100,
		minHeartLine: 1,
		minTemperature: "34.0",
		maxTemperature: "37.0",
		heartLineMonitoringNum: 10,
		temperatureModeNum: 10,
		brightScreen: "开启",
		brightnessList: ["低", "中", "高"],
		brightnessIndex: 0,
		skinColorList: ["白", "白间黄", "黄", "棕色", "褐色", "黑色", "其它"], //肤色 0:白, 1: 白间黄, 2: 黄, 3: 棕色, 4: 褐色, 5: 黑色，7: 其它
		themeList: ["主题一", "主题二"],
		skinColorIndex: 2,
		themeIndex: 0,
		notDisturbState: "禁用",
		notDisturbStartHour: "20",
		notDisturbStartMinute: "00",
		notDisturbEndHour: "08",
		notDisturbEndMinute: "00",
		isEnableTmperatureModeAlert: "true",
		stepGoal: "0",
		sleepHourGoal: "08",
		sleepMinuteGoal: "00",
	},
	notDisturbStartHourChange(e) {
		var hour = e.detail.value
		this.setData({
			notDisturbStartHour: hour
		})
	},
	notDisturbEndHourChange(e) {
		var hour = e.detail.value
		this.setData({
			notDisturbEndHour: hour
		})
	},
	notDisturbStartMinuteChange(e) {
		var minute = e.detail.value
		this.setData({
			notDisturbStartMinute: minute
		})
	},
	notDisturbEndMinuteChange(e) {
		var minute = e.detail.value
		this.setData({
			notDisturbEndMinute: minute
		})
	},
	isclickTime() {
		var that = this
		that.setData({
			timeFlag: that.data.timeFlag ? false : true
		})
	},
	isClickDeviceAntiLost() {
		var that = this
		that.setData({
			antiLostFlag: that.data.antiLostFlag ? false : true
		})
	},
	// mark:------------------------
	// mark：详细设置项显示隐藏事件
	isClick(e) {
		var that = this
		const ds = e.currentTarget.dataset
		if (ds.item == "heartLineFlag") {
			that.setData({
				heartLineFlag: that.data.heartLineFlag ? false : true
			})
		}
		if (ds.item == "heartLineMonitoringFlag") {
			that.setData({
				heartLineMonitoringFlag: that.data.heartLineMonitoringFlag ? false : true
			})
		}
		if (ds.item == "languageFlag") {
			that.setData({
				languageFlag: that.data.languageFlag ? false : true
			})
		}
		if (ds.item == "brightScreenFlag") {
			that.setData({
				brightScreenFlag: that.data.brightScreenFlag ? false : true
			})
		}
		if (ds.item == "brightnessFlag") {
			that.setData({
				brightnessFlag: that.data.brightnessFlag ? false : true
			})
		}
		if (ds.item == "notDisturbFlag") {
			that.setData({
				notDisturbFlag: that.data.notDisturbFlag ? false : true
			})
		}
		if (ds.item == "temperatureFlag") {
			that.setData({
				temperatureFlag: that.data.temperatureFlag ? false : true
			})
		}
		if (ds.item == "tmperatureModeFlag") {
			that.setData({
				tmperatureModeFlag: that.data.tmperatureModeFlag ? false : true
			})
		}
		if (ds.item == "stepGoalFlag") {
			that.setData({
				stepGoalFlag: that.data.stepGoalFlag ? false : true
			})
		}
		if (ds.item == "sleepGoalFlag") {
			that.setData({
				sleepGoalFlag: that.data.sleepGoalFlag ? false : true
			})
		}
	},
	// mark:确认提交修改手表时间事件
	confirmTime() {
		var that = this
		handleToast("命令发送中……", "loading", 0)

		setAPI.setDeviceTime(
			Number(that.data.nowData[0]),
			Number(that.data.nowData[1]),
			Number(that.data.nowData[2]),
			Number(that.data.nowData[3]),
			Number(that.data.nowData[4]),
			Number(that.data.nowData[5]),
			function (state, response) {

				if (state != constant.succeed) {
					return
				}
				that.setData({
					canvasText: that.data.canvasText + "\rsetTimeResponse:" + JSON.stringify(response)
				})
				util.printLog(response)
				closeToast()
			}
		)

	},
	// mark :-
	// mark : onload
	onLoad: function () {
		//设置默认的年份
		this.setData({
			choose_year: this.data.multiArray[0][0]
		})
	},
	//获取时间日期
	bindMultiPickerChange: function (e) {
		// console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			multiIndex: e.detail.value
		})
		const index = this.data.multiIndex;
		const year = this.data.multiArray[0][index[0]];
		const month = this.data.multiArray[1][index[1]];
		const day = this.data.multiArray[2][index[2]];
		const hour = this.data.multiArray[3][index[3]];
		const minute = this.data.multiArray[4][index[4]];
		const second = this.data.multiArray[5][index[5]];
		// console.log(`${year}-${month}-${day}-${hour}-${minute}`);'
		this.setData({
			nowData: []
		})
		this.setData({
			time: year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second,
			nowData: [year, month, day, hour, minute, second]
		})
		// console.log(this.data.time);
	},
	// 点击了设置手表位置选项时间
	isclickWatchLocation() {
		var that = this
		that.setData({
			locationFlag: that.data.locationFlag ? false : true
		})

	},
	// 监听改变了手表佩戴位置事件
	onLocationChange(e) {
		var that = this;
		const detail = e.detail;
		that.setData({
			'watchLocation': detail.value
		})
		util.printLog("点击了切换佩戴位置", that.data.locationFlag)
		handleToast("命令发送中", "loading", 0)
		setAPI.setDeviceWearingPosition(that.data.locationFlag ? false : true, (state, res) => {
			util.printLog("执行结果", state)
			util.printLog("执行结果", res)
			closeToast()
			if (state == constant.succeed) {
				that.setData({
					canvasText: that.data.canvasText + "\n 设置佩戴位置:" + "true"
				})
			} else {
				that.setData({
					canvasText: that.data.canvasText + "\n 设置佩戴位置:" + "false"
				})
			}
		})
	},
	// mark: ------------------
	// mark : 执行防丢命令
	isEnableDeviceAntiLost(e) {
		var that = this;
		const detail = e.detail;
		that.setData({
			'isEnableAntiLost': detail.value
		})

		handleToast("命令发送中", "loading", 0)
		setAPI.setDeviceAntiLost(that.data.isEnableAntiLost ? false : true, (state, res) => {
			closeToast()
			util.printLog("执行结果", state)
			util.printLog("执行结果", res)
			that.setData({
				canvasText: that.data.canvasText + "\n 设置防丢:" + "success"
			})
		})
	},
	// mark: 心率报警开关选择器事件-onchangeHeardLine
	onchangeHeardLine(e) {
		var that = this;
		const detail = e.detail;
		that.setData({
			'isEnableheartLineAlert': detail.value
		})
	},
	// mark: 温度报警开关选择器事件-onchangeHeardLine
	onchangeTemperature(e) {
		var that = this;
		const detail = e.detail;
		that.setData({
			'isEnableTemperatureAlert': detail.value
		})
	},
	// mark: 最大心率阈值 监听事件-handleChangeMaxHeardLine
	handleChangeMaxHeardLine({
		detail
	}) {
		var that = this
		var max = detail.value
		util.printLog("max", max)
		that.setData({
			maxHeartLine: "max"
		})
	},
	// mark: 最小心率阈值 监听事件-handleChangeMaxHeardLine
	handleChangeMinHeardLine({
		detail
	}) {
		var that = this
		var min = detail.value
		util.printLog("min", min)
		that.setData({
			minHeartLine: min
		})
	},
	// mark: 监听输入最小温度值
	handleChangeMinTemperature({
		detail
	}) {
		var that = this
		var min = detail.detail.value
		that.setData({
			minTemperature: min
		})
	},
	// mark: 监听输入最大温度值
	handleChangeMaxTemperature({
		detail
	}) {
		var that = this
		var max = detail.detail.value
		that.setData({
			maxTemperature: max
		})
	},
	// mark: 提交心率报警
	confirmHeardLine() {
		var that = this
		handleToast("命令执行中", "loading", 0)
		setAPI.setDeviceHeartRateAlarm(
			that.data.isEnableheartLineAlert,
			that.data.maxHeardLine,
			that.data.minHeardLine,
			(state, res) => {
				util.printLog("解析结果：", state)
				util.printLog("解析结果：", res)
				that.setData({
					canvasText: that.data.canvasText + "\n 设置心率报警: success"
				})
				closeToast()
			})
	},
	// mark: 提交温度报警
	confirmtemperature() {
		var that = this
		handleToast("命令执行中", "loading", 0)
		var highTemperatureIntegerValue = that.data.maxTemperature.substr(0, that.data.maxTemperature.indexOf('.'))
		var lowTemperatureIntegerValue = that.data.maxTemperature.substr(that.data.maxTemperature.indexOf('.') + 1)
		var highTemperatureDecimalValue = that.data.minTemperature.substr(0, that.data.minTemperature.indexOf('.'))
		var lowTemperatureDecimalValue = that.data.minTemperature.substr(that.data.minTemperature.indexOf('.') + 1)

		setAPI.setDeviceTemperatureAlarm(
			that.data.isEnableTemperatureAlert,
			highTemperatureIntegerValue,
			lowTemperatureIntegerValue,
			highTemperatureDecimalValue,
			lowTemperatureDecimalValue,
			(state, res) => {
				util.printLog("解析结果：", state)
				util.printLog("解析结果：", res)
				if (state == 0) {
					that.setData({
						canvasText: that.data.canvasText + "\n设置温度报警: success"
					})
				} else {
					that.setData({
						canvasText: that.data.canvasText + "\n设置温度报警: fail"
					})
				}
				closeToast()
			})
	},
	// mark: 选择自动心率监测或 手动
	onchangeHeartLineMonitoring(e) {
		var that = this;
		const detail = e.detail;
		that.setData({
			isEnableHeartLineMonitoringAlert: detail.value
		})
	},
	// mark: 选择自动温度监测或 手动
	onchangeTmperatureMode(e) {
		var that = this;
		const detail = e.detail;
		that.setData({
			isEnableTmperatureModeAlert: detail.value
		})
	},
	// mark: 设置心率监测间隔时间事件
	handleChangeHeartLineMonitoring({
		detail
	}) {
		var that = this
		var monitoring = detail.value
		util.printLog("monitoring", monitoring)
		that.setData({
			heartLineMonitoringNum: monitoring
		})
	},
	// mark: 设置温度监测间隔时间事件
	handleChangeTmperatureMode({
		detail
	}) {
		var that = this
		var temperature = detail.value
		that.setData({
			temperatureModeNum: temperature
		})
	},
	// mark: 提交设置心率监测模式事件
	confirmHeartLineMonitoring() {
		var that = this
		handleToast("命令执行中", "loading", 0)
		setAPI.setDeviceHeartRateMonitoringMode(
			that.data.isEnableHeartLineMonitoringAlert,
			that.data.heartLineMonitoringNum,
			(state, res) => {
				util.printLog("解析结果：", state)
				util.printLog("解析结果：", res)
				that.setData({
					canvasText: that.data.canvasText + "\n 设置心率监测模式: success"
				})
				closeToast()
			})

	},
	// mark: 提交设置温度监测模式
	confirmTmperatureMode() {
		var that = this
		handleToast("命令执行中", "loading", 0)
		setAPI.setDeviceTemperatureMonitoringMode(
			that.data.isEnableTmperatureModeAlert,
			that.data.temperatureModeNum,
			(state, res) => {
				util.printLog("解析结果：", state)
				util.printLog("解析结果：", res)
				if (state == 0) {
					that.setData({
						canvasText: that.data.canvasText + "\n 设置温度监测模式: success"
					})
				} else {
					that.setData({
						canvasText: that.data.canvasText + "\n 设置温度监测模式: fail"
					})
				}

				closeToast()
			})

	},
	// mark:-------------------------
	// mark: 恢复出厂设置
	resetWatch() {
		var that = this
		handleToast("命令执行中", "loading", 0)
		setAPI.setDeviceReset((state, res) => {
			if (state == 0) {
				util.printLog("解析结果：", state)
				util.printLog("解析结果：", res)
				that.setData({
					canvasText: that.data.canvasText + "\n 恢复出厂设置: success"
				})
			} else {
				that.setData({
					canvasText: that.data.canvasText + "\n 恢复出厂设置: fail"
				})
			}
			closeToast()
		})

	},
	// mark:-------------------------
	// mark: 语言监听事件
	handleLanguageChange({
		detail
	}) {
		handleToast("命令执行中", "loading", 0)
		var that = this
		that.setData({
			currentLanguage: detail.value
		});
		let val = '0x01'
		if (detail.value == "中文") {
			val = '0x01'
		} else if (detail.value == "英文") {
			val = '0x00'
		}
		setAPI.setDeviceLanguage(val, (state, res) => {
			if (state == 0) {
				that.setData({
					canvasText: that.data.canvasText + "\n 设置: " + detail.value + ": success"
				})
			} else {
				that.setData({
					canvasText: that.data.canvasText + "\n 设置: " + detail.value + ": fail"
				})
			}
			closeToast()
		})

	},
	// mark:------------------------- 
	// mark : 执行设置抬腕亮屏指令
	handleBrightScreenChange({
		detail
	}) {
		var that = this
		let status = detail.value
		handleToast("命令执行中", "loading", 0)
		if (status == "开启") {
			status = true
		} else {
			status = false
		}
		that.setData({
			brightScreen: detail.value
		})
		setAPI.setDeviceWristBrightScreen(status, (state, res) => {
			closeToast()
			if (state == 0) {
				that.setData({
					canvasText: that.data.canvasText + "\n" + detail.value + "抬腕亮屏：success"
				})
			} else {
				that.setData({
					canvasText: that.data.canvasText + "\n" + detail.value + "抬腕亮屏：fail"
				})
			}
		})
	},
	// mark:-------------------------
	// mark : 执行设置屏幕亮度指令
	brigthessChang({
		detail
	}) {
		var that = this
		util.printLog("1111", detail.value)
		that.setData({
			brightnessIndex: detail.value
		})
		handleToast("命令执行中", "loading", 0)
		setAPI.setDeviceDisplayBrightness(detail.value, (state, res) => {
			closeToast()
			util.printLog("设置亮度放回", res)
			if (state == 0) {
				that.setData({
					canvasText: that.data.canvasText + "\n 设置屏幕亮度: " + (detail.value == 0 ? '低' : detail.value == 1 ? '中' : '高')
				})
			} else {
				that.setData({
					canvasText: that.data.canvasText + "\n 设置屏幕亮度： fail"
				})
			}
		})
	},
	// mark:-------------------------
	// mark : 执行设置皮肤颜色
	skinColorChang({
		detail
	}) {
		var that = this
		that.setData({
			skinColorIndex: detail.value
		})
		handleToast("命令执行中", "loading", 0)
		setAPI.setDeviceSkinColor(detail.value, (state, res) => {
			closeToast()
			if (state == 0) {
				that.setData({
					canvasText: that.data.canvasText + "\n 设置肤色: success"
				})
			} else {
				that.setData({
					canvasText: that.data.canvasText + "\n 设置肤色: fail"
				})
			}
		})
	},
	// mark:-------------------------
	// mark: 主题设置
	themeChang({
		detail
	}) {
		var that = this
		that.setData({
			themeIndex: detail.value
		})
		handleToast("命令执行中", "loading", 0)
		setAPI.setDeviceTheme(detail.value, (state, res) => {
			closeToast()
			if (state == 0) {
				that.setData({
					canvasText: that.data.canvasText + "\n 设置主题: success"
				})
			} else {
				that.setData({
					canvasText: that.data.canvasText + "\n 设置主题: fail"
				})
			}
		})
	},
	// mark:-------------------------
	// mark: 是否启用勿扰模式
	notDisturbChange({
		detail
	}) {
		var that = this
		let status = detail.value
		that.setData({
			notDisturbState: status
		})
	},
	// mark: 提交勿扰模式设置
	confirmNotDisturb() {
		var that = this
		handleToast("命令执行中", "loading", 0)
		setAPI.setDeviceNotDisturb(
			that.data.notDisturbState == "开启" ? 1 : 0,
			that.data.notDisturbStartHour,
			that.data.notDisturbStartMinute,
			that.data.notDisturbEndHour,
			that.data.notDisturbEndMinute,
			(state, res) => {

				if (state == 0) {
					that.setData({
						canvasText: that.data.canvasText + "\n" + that.data.notDisturbState + "勿扰模式：success"
					})
				} else {
					that.setData({
						canvasText: that.data.canvasText + "\n" + that.data.notDisturbState + "勿扰模式：fail"
					})
				}
				closeToast()


			})
	},
	// mark:-------------------------
	// mark: 修改步数目标输入框监听
	stepGoalChange({
		detail
	}) {
		var that = this
		let stepGoal = detail.detail.value
		that.setData({
			stepGoal: stepGoal
		})
	},
	// mark : 提交步数目标命令
	confirmStepGoal() {
		var that = this
		handleToast("命令执行中", "loading", 0)
		setAPI.setDeviceStepGoal(that.data.stepGoal, (state, res) => {

			if (state == 0) {
				that.setData({
					canvasText: that.data.canvasText + "\n设置步数目标：success"
				})
			} else {
				that.setData({
					canvasText: that.data.canvasText + "\n设置步数目标：fail"
				})
			}
			closeToast()
		})
	},
	// mark: -----
	// mark: 监听输入睡眠目标 小时：分钟
	sleepHourGoalChange({
		detail
	}) {
		var that = this
		let sleepHourGoal = detail.detail.value
		that.setData({
			sleepHourGoal: sleepHourGoal
		})
	},
	sleepMinuteGoalChange({
		detail
	}) {
		var that = this
		let sleepMinuteGoal = detail.detail.value
		that.setData({
			sleepMinuteGoal: sleepMinuteGoal
		})
	},
	// mark: 提交睡眠目标
	confirmSleepGoal() {
		var that = this
		handleToast("命令执行中", "loading", 0)
		setAPI.setDeviceSleepGoal(that.data.sleepHourGoal, that.data.sleepMinuteGoal, (state, res) => {
			if (state == 0) {
				that.setData({
					canvasText: that.data.canvasText + "\n设置睡眠目标：success"
				})
			} else {
				that.setData({
					canvasText: that.data.canvasText + "\n设置睡眠目标：fail"
				})
			}
			closeToast()
		})
	},


	// mark : 设置久坐
	sedentaryReminderSetting() {
		var that = this
		// 8:30 ~ 12:30 14:00 ~ 18:30 每隔15分钟提醒一次，一周七天都提醒
		setAPI.setDeviceSedentary(
			8,
			30,
			12,
			30,
			14,
			0,
			18,
			30,
			15,
			[ constant.repeatMonday,
				constant.repeatTuesday,
				constant.repeatWednesday,
				constant.repeatThursday,
				constant.repeatFriday,
				constant.repeatSaturday,
				constant.repeatSunday,
				constant.repeatEnable,
			],

			function (state, response) {
				if (state == constant.succeed) {
					// 设置成功
					that.setData({
						canvasText: that.data.canvasText + "\n设置久坐：success"
					})
				} else {
					// 设置失败
					that.setData({
						canvasText: that.data.canvasText + "\n设置久坐：fail"
					})
				}
			})
	},

	// mark : 设置单位
	unitSetting() {
		var that = this
		setAPI.setDeviceUnit(0, 0, 0, 0, function (state, response) {
			if (state == constant.succeed) {
				// 设置成功
				that.setData({
					canvasText: that.data.canvasText + "\n设置单位：success"
				})
			} else {
				// 设置失败
				that.setData({
					canvasText: that.data.canvasText + "\n设置单位：fail"
				})
			}
		})

	},

	// mark : 设置个人信息
	userInfoSeting() {
		var that = this
		setAPI.setDeviceUserInfo(160, 60, 0, 20, function (state, response) {
			if (state == constant.succeed) {
				// 设置成功
				that.setData({
					canvasText: that.data.canvasText + "\n设置个人信息：success"
				})
			} else {
				// 设置失败
				that.setData({
					canvasText: that.data.canvasText + "\n设置个人信息：fail"
				})
			}
		})

  },
  

  // MARK: - 发送健康内容
    
  /**
   * 发送健康内容
   */
  sendHealthInfo() {

    var that = this
    handleToast("发送健康数据", "loading", 0)
  
    HealthContentAPI.sendHealthContent(
       constant.YCDistributeHealthInfoTypeHealthAdvice, 
      [

        ['合理运动健身', '坚持日常身体活动，每周至少进行5天中等强度身体活动，累计150分钟以上主动身体活动最好每天6000渐步。'],
        
        ['戒烟限酒渐', '烟草与酒精都会对健康造成渐损害。为了自己和家人的健康，建议立即采取相关措施来戒烟与控制饮酒，建议成年男性每日饮用的酒精量不超过25克(约1两高度白酒)，女性不超过15克(约1两中度白酒)'],

        ['少盐更健康', '当前的口味渐较重，说明钠盐摄入较多。这会导致心血管病的风险上升。建议逐渐减少食盐的摄入量，每日不多于6克 '],
        
      ],
      function (state, response){
        closeToast()
        that.setData({
          canvasText: that.data.canvasText + "\n发送健康数据: " + JSON.stringify(response)
        })
      });
  },

  // 设置女性生理周期
  settingFemalePhysiologicalCycle(){
    handleToast("发送健康数据", "loading", 0)
    var that = this
    const timestamp = 1747214859     // 经期时间
    const durations = 7              // 持续天数
    const cycleDays = 30             // 周期天数
    setAPI.setFemalePhysiologicalCycle(timestamp, durations, cycleDays, (state, res) => {

      if (state == 0) {
        that.setData({
          canvasText: that.data.canvasText + "\n设置女性生理周期：success"
        })
      } else {
        that.setData({
          canvasText: that.data.canvasText + "\n设置女性生理周期：fail"
        })
      }
      closeToast()
    })

  },


})